package SqlJavaBean;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;
import java.util.Properties;

public class DBBean {
	private String driver;
	private String url;
	private String user;
	private String passwd;
	private List<Connection> connect;    //拟提供数据库连接管理
	private final int connect_count = 20;  //最大连接数
	
	public DBBean(){
		String sp = File.separator;
		String pathx = System.getProperty("user.dir")+sp+"myConfigure"+sp+"db.properties";
		String p = "/home/sshh/tomcat/webapps/SQL-online/myConfigure/db.properties";
		String path = System.getProperty("user.dir");
		path = path.substring(0, path.lastIndexOf("/"))+"/webapps/SQL-online/WEB-INF/myConfigure/db.properties";
		initParam(pathx);
	}
	public String getPath(){
		String sp = File.separator;
		String pathx = System.getProperty("user.dir");
		return pathx;
	}
	
	private boolean initParam(String paramPath){
		try{
			Properties pro = new Properties();
			pro.load(new FileInputStream(paramPath));
			this.driver = pro.getProperty("driver");
			this.url = pro.getProperty("url");
			this.user = pro.getProperty("user");
			this.passwd = pro.getProperty("passwd");
			
		}
		catch(Exception e){
			return false;
		}
		return true;
	}
	
	public Connection getConnection(){
		Connection con = null;
		try{
			Class.forName(driver).newInstance();
			con = DriverManager.getConnection(url,user,passwd);
		}
		catch(Exception e){
			return null;
		}
		return con;
	}
	
	public Connection getConnection(String user,String passwd){
		String url = "jdbc:mysql://localhost:3306/"+user;
		Connection con = null;
		try{
			Class.forName(driver).newInstance();
			con = DriverManager.getConnection(url,user,passwd);
		}
		catch(Exception e){
			return null;
		}
		return con;
	}

	public boolean closeConnect(){
		return true;
	}
	
	public static void main(String[] s){
		DBBean db = new DBBean();
		String sp = File.separator;
		String path = db.getPath();
		path = path.substring(0, path.lastIndexOf("/"))+"/webapps/SQL-online/WEB-INF/myConfigure/db.properties";
		System.out.println(path);
		
	}
	
	
}
